"""
Copyright (c) 2023 - present SKbioinfo
"""

from urllib import response
from flask_restful import Resource
from flask import jsonify,request,jsonify
from apps.API.utils.log_helper import lg
from apps.API.connector.db_common import *
from apps.API.common.common_time import *
from apps.API.common.db_table_config import *
from apps.API.auth.util import hash_pass
       
class interfaceUser(Resource):
    # @login_required
    def get(self):
        try:
            ### SQL: query user info ###
            query_user_sql = '''
                SELECT 
                    tb1.id,tb1.username,tb1.email,tb1.role_id,
                    tb1.create_time,tb1.modify_time,
                    tb2.group,tb2.detail,tb2.permissions
                FROM {table_one} as tb1 
                LEFT JOIN {table_two} as tb2 
                ON tb1.{col_one} = tb2.{col_two} 
                WHERE tb1.username != "admin"
            '''.format(
                table_one=common_tables.users_db["name"],
                table_two=common_tables.role_db["name"],
                col_one="role_id",
                col_two="name"
            )
            base_infos=dic_query_info(query_user_sql)
            response=jsonify(base_infos)
            response.status=202
            return response
        except Exception as e:
            lg.error(e)
            return 400
        
    def post(Resource):
        try:
            ## SQL: insert User script ###
            cols= '''
                username,
                email,
                role_id,
                create_time,
                modify_time,
                password
            '''
            value='''
                \"{username}\",
                \"{email}\",
                \"{role_id}\",
                \"{create_time}\",
                \"{modify_time}\",
                \"{password}\"
            '''.format(
                username=request.json["username"],
                email=request.json["email"],
                role_id=request.json["details"].split(":")[0],
                create_time=get_current_time(),
                modify_time=get_current_time(),
                password=hash_pass(request.json["password"]).decode()
            )
            ### SQL: add user sql ###
            add_user_sql = '''
                INSERT INTO {table_name} 
                ({cols}) VALUES ({value})
            '''.format(
                table_name=common_tables.users_db["name"],
                cols=cols,
                value=value
            )
            add_info(add_user_sql)
        except Exception as e:
            lg.error(e)
            return 400

class interfaceUserID(Resource):
     def delete(self,id):
        try:
            ### SQL: delete user in database ###
            delete_sql = '''DELETE FROM {table_name} WHERE {db_name} = \"{col_value}\"'''.format(
                table_name=common_tables.users_db["name"],
                col_value=id,
                db_name="id"
            )
            delete_info(delete_sql = delete_sql)
            return 202
        except Exception as e:
            lg.error(e)
            
class interfaceRole(Resource):
    def get(self):
        try:
            cols = '''
                id,
                name,
                detail
            '''
            ### SQL: query role infos ###
            role_info_sql ='''
                SELECT {cols} FROM {table_name} 
            '''.format(
                cols=cols,
                table_name=common_tables.role_db["name"]
            )
            base_info=dic_query_info(role_info_sql)
            role_info = []
            for info in base_info:
                if info["name"] == 'R990':
                    pass
                else:
                    role_info.append(info)
            response=jsonify(role_info)
            response.status=202
            return response
        except Exception as e:
            lg.error(e)

class interfaceQueryUser(Resource):
    def post(self):
        try:
            cols = '''
                tb1.id,tb1.username,tb1.email,tb1.role_id,
                tb1.create_time,tb1.modify_time,
                tb2.group,tb2.detail,tb2.permissions
            '''
            user_infos = []
            for user_id in request.json["user_id"]:
                ### SQL: query user info ###
                query_user_info_sql = '''
                    SELECT 
                        {cols}
                    FROM {table_one} as tb1
                    LEFT JOIN {table_two} as tb2
                    ON tb1.role_id = tb2.name
                    WHERE {col_name} = \"{user_id}\"
                '''.format(
                    cols=cols,
                    table_one=common_tables.users_db["name"],
                    table_two=common_tables.role_db["name"],
                    col_name="tb1.username",
                    user_id=user_id
                )
                base_info=dic_query_info(query_user_info_sql)
                user_infos.append(base_info[0])
            response=jsonify(user_infos)
            response.status=202
            return response
        except Exception as e:
            lg.error(e)
            return 404
